package com.mdd.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {

    // 1.1 获取工厂对象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource ds){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        // 1.2 设置实体类的别名
        ssfb.setTypeAliasesPackage("com.mdd.domain");   // 实体类所在包的位置
        // 1.3 设置数据源
        ssfb.setDataSource(ds);
        return ssfb;
    }

    // 2 获取Mapper 代理对象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.mdd.mapper");     // 代理类包的位置
        return msc;
    }

}
